package hhm.android.account

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.alibaba.android.arouter.facade.annotation.Route
import hhm.android.account.databinding.ActivityForgetPasswordBinding
import hhm.android.library.bus.Bus
import hhm.android.library.utils.MatchUtils
import siau.android.base.FORGET_PASSWORD_ROUTE
import siau.android.base.SBBaseActivity
import siau.android.library.KeyString

@Route(path = FORGET_PASSWORD_ROUTE)
class ForgetPasswordActivity : SBBaseActivity(), TextWatcher {

    lateinit var viewModel: ForgetPasswordViewModel
    lateinit var dataBinding: ActivityForgetPasswordBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_forget_password)
        initTitle()
        viewModel = ViewModelProvider(this).get(ForgetPasswordViewModel::class.java)
        dataBinding.viewModel = viewModel
        dataBinding.lifecycleOwner = this

        viewModel.canClickBtn.observe(this, {
            if (it) {
                dataBinding.activityForgetPasswordBtn.background = ContextCompat.getDrawable(this, R.drawable.rectangle_circle_maincolor)
            } else {
                dataBinding.activityForgetPasswordBtn.background = ContextCompat.getDrawable(this, R.drawable.rectangle_circle_grey_e6)
            }
        })

        viewModel.errorString.observe(this, {
            removeLoadingFragment()
            showToast(it)
        })

        viewModel.countDownNumber.observe(this, {
            if (it == 60) {
                dataBinding.activityForgetPasswordGetCode.text = getString(R.string.get_verify_code)
            } else {
                removeLoadingFragment()
                dataBinding.activityForgetPasswordGetCode.text = getString(R.string.re_get_verify_code_1) + it + getString(R.string.re_get_verify_code_2)
            }
        })
        viewModel.passwordChange.observe(this, {
            removeLoadingFragment()
            if (it) {
                showToast(getString(R.string.password_change_success_and_relogin))
                finish()
                Bus.getDefault().post(KeyString.passwordChange)
            }
        })

        dataBinding.activityForgetPasswordAccountEt.addTextChangedListener(this)
        dataBinding.activityForgetPasswordCodeEt.addTextChangedListener(this)
        dataBinding.activityForgetPasswordPasswordEt.addTextChangedListener(this)

        dataBinding.activityForgetPasswordPasswordCanSee.setOnClickListener {
            viewModel.showPassword = !viewModel.showPassword
            if (viewModel.showPassword) {
                dataBinding.activityForgetPasswordPasswordEt.transformationMethod = HideReturnsTransformationMethod.getInstance()
                dataBinding.activityForgetPasswordPasswordCanSee.setImageResource(R.mipmap.can_see)
            } else {
                dataBinding.activityForgetPasswordPasswordEt.transformationMethod = PasswordTransformationMethod.getInstance()
                dataBinding.activityForgetPasswordPasswordCanSee.setImageResource(R.mipmap.can_not_see)
            }
            dataBinding.activityForgetPasswordPasswordEt.setSelection(dataBinding.activityForgetPasswordPasswordEt.text.length)
        }

        dataBinding.activityForgetPasswordGetCode.setOnClickListener {
            if (viewModel.canGetCode) {
                if (!viewModel.hasGetCode.value!!) {
                    viewModel.hasGetCode.value = true
                    viewModel.startCountDown(lifecycle(), dataBinding.activityForgetPasswordAccountEt.text.toString())
                }
            } else {
                showToast(getString(R.string.please_input_true_phone))
            }
        }
        dataBinding.activityForgetPasswordBtn.setOnClickListener {
            if (viewModel.canClickBtn.value!!) {
                showLoadingFragment(R.id.activity_forget_password_fl)
                viewModel.changePassword(lifecycle(), dataBinding.activityForgetPasswordAccountEt.text.toString(), dataBinding.activityForgetPasswordCodeEt.text.toString(), dataBinding.activityForgetPasswordPasswordEt.text.toString())
            }
        }
    }

    override fun finish() {
        viewModel.finish()
        super.finish()
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        viewModel.canGetCode = MatchUtils.isMobile(dataBinding.activityForgetPasswordAccountEt.text.toString())
        viewModel.canClickBtn.value = viewModel.canGetCode && dataBinding.activityForgetPasswordCodeEt.text.length == 6 && dataBinding.activityForgetPasswordPasswordEt.text.length >= 6
    }

    override fun afterTextChanged(s: Editable?) {
    }
}